﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetMigrations.Commands
{
	[ConsoleCommand("migrate")]
	public class MigrateCmd : ConsoleCommand
	{
		public MigrateCmd(string[] args) : base(args) { }

		private int _targetVersion = -1;

		public override bool ValidateArgs()
		{
			if (ArgCount < 1)
			{
				Output.Warn("Connection string not found");
				return false;
			}

			DAL.ConnectionString = ArgFetcher.GetConnectionString(Args);

			if (ArgCount >= 2)
			{
				_targetVersion = ArgFetcher.GetFirstNumericArg(Args);
			}

			return true;
		}

		public override bool Run()
		{
			if (_targetVersion >= 0)
				return Database.Migrate(_targetVersion);
			else
				return Database.Migrate();
		}
	}
}
